package time.table;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.view.View;
import android.os.Bundle;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;

import android.net.http.AndroidHttpClient;

import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;

import android.os.AsyncTask;

public class TimeTableActivity extends Activity {
    private String yourAuthValue;
	private Boolean autorized;
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    public void messageBox(String messageText){
    	messageBox(messageText, "Time Table");
    }
    public void messageBox(String messageText, String messageTitle){
    	AlertDialog alertDialog;
    	alertDialog = new AlertDialog.Builder(this).create();
    	alertDialog.setTitle(messageTitle);
    	alertDialog.setMessage(messageText);
    	
    	alertDialog.setButton("OK", new OnClickListener(){
    		@Override
    		public void onClick(DialogInterface dialog, int which) {               
    			dialog.cancel();
    		}
    		});
    	alertDialog.show();
    }
    public void button1_onclick(View v) {
    	 new DownloadImageTask().execute("https://www.google.com/accounts/ClientLogin");
    }

	private class DownloadImageTask extends AsyncTask <String, String, String> {
	     protected String doInBackground(String... urls) {
	         return loadImageFromNetwork(urls[0]);
	     }

	     protected void onPostExecute(String result) {
	        messageBox(result); // mImageView.setImageBitmap(result);
	     }
	 }
	public String loadImageFromNetwork(String s){
		return downloadData(s);
	}
	
	
    public String downloadData(String url) {
        final AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
        final HttpPost getRequest = new HttpPost(url);
        
        if (!this.autorized){
        	url = "https://www.google.com/accounts/ClientLogin";
	        List<NameValuePair> pairs = new ArrayList<NameValuePair>();
	        pairs.add(new BasicNameValuePair("accountType", "HOSTED_OR_GOOGLE"));
	        pairs.add(new BasicNameValuePair("Email", "****@gmail.com"));
	        pairs.add(new BasicNameValuePair("Passwd", "***"));
	        pairs.add(new BasicNameValuePair("service", "cl"));
	        pairs.add(new BasicNameValuePair("source","ddd-TimeStamp-1.05"));
	        
	        
	        try {
				getRequest.setEntity(new UrlEncodedFormEntity(pairs));
			} catch (UnsupportedEncodingException e1) {
				e1.printStackTrace();
			}
        }else{
            getRequest.setHeader("Authorization", "GoogleLogin auth=" + yourAuthValue);
        }
        //Authorization: yourAuthValue
        
        //pairs.add(new BasicNameValuePair("key1", "value1"));
        //pairs.add(new BasicNameValuePair("key2", "value2"));
       

        try {
            HttpResponse response = client.execute(getRequest);
            final int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != HttpStatus.SC_OK) { 
            	return "Error " + statusCode + " while retrieving bitmap from " + url; 
            }
            final HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream inputStream = null;
                try {
                    inputStream = entity.getContent();
                    BufferedReader rd = new BufferedReader(new
                    InputStreamReader(inputStream));
                   
                    String line = "", res="";
                    if (this.autorized){
	                    while ((line = rd.readLine()) != null) {
	                    	res += "\n" + line; 
	                    } 
                    } else{
                    	while ((line = rd.readLine()) != null) {
	                    	res += "\n" + line; 
	                    }
                    }
                    return res ;
                } finally {
                    if (inputStream != null) {
                        inputStream.close();  
                    }
                    entity.consumeContent();
                }
            }
        } catch (Exception e) {
            // Could provide a more explicit error message for IOException or IllegalStateException
            getRequest.abort();
        //    messageBox("Error while retrieving data from " + url + " " + e.toString());
        } finally {
            if (client != null) {
                client.close();
            }
        }
        return null;
    }
    public void button2_onclick(View v) {
    	messageBox("Message Text", "Message Title");
    }
}
